package mapper

import (
	"context"
	"gorm.io/gorm"
	"mall/services/product/rpc/internal/domain/bo"
	"mall/services/product/rpc/internal/initialize/mysql"
	"mall/services/product/rpc/internal/mapper/entity"
)

var spuDetailAllColumn = "spu_id, create_time, update_time, detail"

type SpuDetailMapper struct {
}

func NewSpuDetailMapper() *SpuDetailMapper {
	return &SpuDetailMapper{}
}
func (m *SpuDetailMapper) SelectCountCtx(ctx context.Context, db *gorm.DB, spuBO bo.SpuDetailBO) (totalCount int64, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuDetailMapper#SelectCountCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	m.getAllWhereSql(tx, spuBO)
	var total int64
	userEntity := entity.SpuDetailEntity{}
	tx = tx.Select(spuAllColumn).Table(userEntity.TableName()).Count(&total)
	return total, tx.Error
}
func (m *SpuDetailMapper) SelectOneCtx(ctx context.Context, db *gorm.DB, spuBO bo.SpuDetailBO) (resp entity.SpuDetailEntity, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuDetailMapper#SelectCountCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	spuDetailEntity := entity.SpuDetailEntity{}
	tx = tx.Select(spuDetailAllColumn).Table(spuDetailEntity.TableName())
	m.getAllWhereSql(tx, spuBO)
	tx = tx.Find(&spuDetailEntity)
	return spuDetailEntity, tx.Error
}
func (m *SpuDetailMapper) SelectListCtx(ctx context.Context, db *gorm.DB, spuBO bo.SpuDetailBO) (respEntityArr []entity.SpuDetailEntity, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuDetailMapper#SelectListCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	var respArr []entity.SpuDetailEntity
	detailEntity := entity.SpuDetailEntity{}
	tx = tx.Select(spuDetailAllColumn).Table(detailEntity.TableName())
	m.getAllWhereSql(tx, spuBO)
	tx = tx.Find(&respArr)
	return respArr, tx.Error
}

func (m *SpuDetailMapper) getAllWhereSql(db *gorm.DB, spuDetailBo bo.SpuDetailBO) {
	if spuDetailBo.SpuId > 0 {
		db.Where("spu_id = ?", spuDetailBo.SpuId)
	}
	if len(spuDetailBo.Detail) > 0 || spuDetailBo.Detail != "" {
		db.Where("detail = ?", spuDetailBo.Detail)
	}
	if spuDetailBo.StartTime != nil {
		db.Where("create_time = ?", spuDetailBo.StartTime)
	}
	if spuDetailBo.EndTime != nil {
		db.Where("create_time = ?", spuDetailBo.EndTime)
	}
}

func (m *SpuDetailMapper) InsertCtx(ctx context.Context, engin *gorm.DB, detailEntity entity.SpuDetailEntity) (spuId int64, err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuDetailMapper#InsertCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx := engin.Create(&detailEntity)
	return detailEntity.SpuId, tx.Error
}

func (m *SpuDetailMapper) UpdateCtx(ctx context.Context, tx *gorm.DB, detailEntity entity.SpuDetailEntity) (err error) {
	// 监控
	ctx, span := mysql.StartSpan(ctx, "SpuDetailMapper#UpdateCtx")
	defer func() {
		mysql.EndSpan(span, err)
	}()
	tx = tx.Where("spu_id = ?", detailEntity.SpuId).Omit("spu_id").
		UpdateColumns(detailEntity)
	return tx.Error
}
